package com.example.onlinemarket.model;

import android.os.Parcel;
import android.os.Parcelable;

import com.google.gson.annotations.Expose;

import java.util.Objects;

import static com.example.onlinemarket.Application.download;
import static com.example.onlinemarket.util.FileUtil.externalFilesDir;

public class MobileOrderItem implements Parcelable {
    @Expose public int orderId;//订单编号
    @Expose public int goodId;//商品编号
    @Expose public String goodName;//商品名
    @Expose public int goodNum;//订购数量
    @Expose public String imgPath;
    @Expose public String localPath;
    @Expose public double sellPrice;

    protected MobileOrderItem(Parcel in) {
        orderId = in.readInt();
        goodId = in.readInt();
        goodName = in.readString();
        goodNum = in.readInt();
        imgPath = in.readString();
        localPath = in.readString();
        sellPrice = in.readDouble();
    }

    public static final Creator<MobileOrderItem> CREATOR = new Creator<MobileOrderItem>() {
        @Override
        public MobileOrderItem createFromParcel(Parcel in) {
            return new MobileOrderItem(in);
        }

        @Override
        public MobileOrderItem[] newArray(int size) {
            return new MobileOrderItem[size];
        }
    };

    @Override
    public String toString() {
        return "MobileOrderItem{" +
                "orderId=" + orderId +
                ", goodId=" + goodId +
                ", goodName='" + goodName + '\'' +
                ", goodNum=" + goodNum +
                ", sellPrice=" + sellPrice +
                '}';
    }

    //获取商品图片
    public void getGoodImg(){
        download(imgPath);
        localPath = externalFilesDir+imgPath;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof MobileOrderItem)) return false;
        MobileOrderItem that = (MobileOrderItem) o;
        return orderId == that.orderId &&
                goodId == that.goodId &&
                goodNum == that.goodNum &&
                Double.compare(that.sellPrice, sellPrice) == 0 &&
                Objects.equals(goodName, that.goodName) &&
                Objects.equals(imgPath, that.imgPath);
    }

    @Override
    public int hashCode() {
        return Objects.hash(orderId, goodId, goodName, goodNum, imgPath, sellPrice);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(orderId);
        dest.writeInt(goodId);
        dest.writeString(goodName);
        dest.writeInt(goodNum);
        dest.writeString(imgPath);
        dest.writeString(localPath);
        dest.writeDouble(sellPrice);
    }
}